# (1)for 某个变量名 in 可迭代的对象
# 循环或者迭代 字符串，列表、字典等等可迭代的对象
# (2) while - 循环

# 循环 遍历 迭代
# 字符串
# txt = 'hello world'
# for ch in txt:
#     print(ch)

# # 列表
# fruits = ["苹果", "香蕉", "桃子", "橘子"]
# print(type(fruits))
#
# for fruit in fruits:
#     print(fruit)
#
# for idx in range(3, 7):
#     print(idx)

# # break 提前结束循环
# txt = 'hello world'
# for ch in txt:
#     if ch == ' ':
#         break
#     print(ch)
#
# print("Here")

# #continue 跳过本次，继续下一次  skip
# txt = 'hello world'
# for ch in txt:
#     if ch == ' ':
#         continue
#     print(ch)

# # 两者结合
# txt = 'hello world'
# for cr in txt:
#     if cr == ' ':
#         continue
#     if cr == 'r':
#         break
#     print(cr)
# print('DONE')


# num = 0
# while num <= 10:
#     print(num)
#     num = num + 1
#
# print('结束了')

# num = 0
# total = 0
# while num <= 10:
#     total = total + num
#     print('当前数字是',num,'当前计算的和是',total)
#     num = num + 1

# while 死循环

# num = 0
# while num <= 10:
#     print(num)
#     num = num - 1
#
# print('结束了')

# while break
# import random
# random.seed(0)
#
# total = 0
# while total < 10000000:
#     print(total)
#     # 等价于 total = total + random.randrange(20)
#     total += random.randrange(20)
#
#     # 取余数
#     if total % 17 == 1:
#         break
#     #
#     if total ** 2 % 23 == 7:
#         break
#
# print("done")

# while True
#
# import random
# random.seed(0)
# total = 0
# while True:
#     print(total)
#     total += random.randrange(20)
#     if total >= 10000000:
#         break
#
#     if total % 17 == 1:
#         break
#
#     if total ** 2 % 23 == 7:
#         break
#
# print("done")

# year_str = input("请输入你的出生年份")
#
# while len(year_str) != 4:
#     year_str = input("请输入你的出生年份")
#
# print("你的出生年份是" + year_str)

# while True:
#     year_str = input("请输入你的出生年份：")
#     if len(year_str) == 4:
#         break
#
# print("你的出生年份是" + year_str)
# print('123123')
# exit(404)
# print('12312312')

# def my_func():
#     print(111)
#     print(222)
#     return
#     print(111)
#
# my_func()

# • exit will stop your program no matter where you call it.
# • return will return from a function (it will stop the specific function only)
# • break will stop the current “while” or “for” loop
# • continue will stop the current iteration of the current “while” or “for” loop


# 练习：打印字符串中的所有位置
# 给定一个字符串"The black cat climbed the green tree."，打印出来所有"c"字母所在位置的索引 find while
# one_str = 'The black cat climbed the green tree.'
# start = 0  # 每次开始找的位置
#
# while True:
#     loc = one_str.find('c', start)
#     if loc == -1:
#         # 字符串已经找不到c了，就跳出循环了
#         break
#     print(loc)
#     start = loc + 1  # 每次会重新赋值start

# 练习：数字猜测游戏

# level 0
# 使用随机模块，计算机产生一个在1到201之间的整数。
# 用户必须猜测这个数字。在用户输入猜测的数字后，计算机会告诉它这是比它生成的数字大还是小，还是相等。  条件语句
# 游戏只能猜了一次就结束了
# 之前做过了

# level1
# 用户可以猜很多次了。当用户猜对了正确的数字时，游戏才结束了。 while  break
# import random
#
# hidden = random.randrange(1, 201)  # 6
# while True:
#     user_input = input("Please enter your guess: ")
#     print(user_input)
#
#     guess = int(user_input)
#     if guess == hidden:
#         print("Hit!")
#         break
#
#     if guess < hidden:
#         print("Your guess is too low")
#     else:
#         print("Your guess is too high")

# Level 2
# 如果用户输入“x”，则可以离开游戏，而不猜测数字。 if  break
# import random
#
# hidden = random.randrange(1, 201)
# while True:
#     user_input = input("Please enter your guess[x]: ")
#     print(user_input)
#
#     if user_input == 'x':
#         print("Sad to see you leaving early")
#         exit()
#
#     guess = int(user_input)
#     if guess == hidden:
#         print("Hit!")
#         break
#
#     if guess < hidden:
#         print("Your guess is too low")
#
#     else:
#         print("Your guess is too high")

# Level 3
# 如果用户按"s"，则显示答案（用户想作弊了） if continue
# import random
#
# hidden = random.randrange(1, 201)
#
# while True:
#     user_input = input("Please enter your guess [x|s]: ")
#     print(user_input)
#
#     if user_input == 'x':
#         print("Sad to see you leaving early")
#         exit()
#
#     if user_input == 's':
#         print("The hidden value is ", hidden)
#         continue
#
#     guess = int(user_input)
#     if guess == hidden:
#         print("Hit!")
#         break
#
#     if guess < hidden:
#         print("Your guess is too low")
#     else:
#         print("Your guess is too high")
#

# Level 4
# 很快我们就会有一个级别，在每次猜测后，隐藏的值都在发生变化。为了使该模式更容易跟踪和调试，首先我们希望有一个“调试模式”。 debug
# 如果用户按‘d’键，游戏将进入“调试模式”：就在询问用户提供新输入之前，系统开始显示每次猜测的当前数字，
# 再次按下“d”键将关闭调试模式。（它是一个切换，每按“d”将值设置为相反的。）  类似于开关
#
# import random
#
# hidden = random.randrange(1, 201)
# debug = False
# while True:
#     if debug:
#         print("Debug: ", hidden)
#
#     user_input = input("Please enter your guess [x|s|d]: ")
#     print(user_input)
#
#     if user_input == 'x':
#         print("Sad to see you leaving early")
#         exit()
#
#     if user_input == 's':
#         print("The hidden value is ", hidden)
#         continue
#
#     if user_input == 'd':
#         debug = not debug
#         continue
#
#     guess = int(user_input)
#     if guess == hidden:
#         print("Hit!")
#         break
#
#     if guess < hidden:
#         print("Your guess is too low")
#     else:
#         print("Your guess is too high")

# Level 5
# “m”按钮是另一个切换按钮。它叫做“移动模式”。当它是“on”时，隐藏的数字在每一步之后都会有一点变化（范围为-2到2）。 偏移功能
# 再次按“m”将关闭此功能。
#
# import random
#
# hidden = random.randrange(1, 201)
# debug = False
# move = False
# while True:
#     if debug:
#         print("Debug: ", hidden)
#
#     if move:
#         mv = random.randrange(-2, 3)
#         hidden = hidden + mv
#
#     user_input = input("Please enter your guess [x|s|d|m]: ")
#     print(user_input)
#
#     if user_input == 'x':
#         print("Sad to see you leaving early")
#         exit()
#
#     if user_input == 's':
#         print("The hidden value is ", hidden)
#         continue
#
#     if user_input == 'd':
#         debug = not debug
#         continue
#
#     if user_input == 'm':
#         move = not move
#         continue
#
#     guess = int(user_input)
#     if guess == hidden:
#         print("Hit!")
#         break
#
#     if guess < hidden:
#         print("Your guess is too low")
#     else:
#         print("Your guess is too high")

# Level 6
# 让用户玩几次游戏
# 按“n”就会跳过这个游戏，开始一个新的游戏。生成一个要猜测的新数字。 new
# import random
#
# debug = False
# move = False
# while True:
#     print("\nWelcome to another Number Guessing game")
#     hidden = random.randrange(1, 201)
#     while True:
#         if debug:
#             print("Debug: ", hidden)
#
#         if move:
#             mv = random.randrange(-2, 3)
#             hidden = hidden + mv
#
#         user_input = input("Please enter your guess [x|s|d|m|n]: ")
#         print(user_input)
#
#         if user_input == 'x':
#             print("Sad to see you leaving early")
#             exit()
#
#         if user_input == 's':
#             print("The hidden value is ", hidden)
#             continue
#
#         if user_input == 'd':
#             debug = not debug
#             continue
#
#         if user_input == 'm':
#             move = not move
#             continue
#
#         if user_input == 'n':
#             print("Giving up, eh?")
#             break
#
#         guess = int(user_input)
#         if guess == hidden:
#             print("Hit!")
#             break
#         if guess < hidden:
#             print("Your guess is too low")
#         else:
#             print("Your guess is too high")

# 练习：计算唯一的字符
# 一个字符串bcdaaa，计算它有多少种不同的字符 while
# 4
# one_str = 'bcdaaa'
# unique = ''
#
# for c in one_str:
#     if c not in unique:
#         unique = unique + c
#         print(unique)
# print(len(unique))


# 练习：将for-in 循环转换为while循环
for ix in range(3, 17, 3):
    print(ix)

n = 3
while n < 17:
    print(n)
    n = n + 3
